package cn.sfrog.project.biz.admin.controller;

import cn.sfrog.common.annotation.Log;
import cn.sfrog.common.annotation.RepeatSubmit;
import cn.sfrog.common.core.controller.BaseController;
import cn.sfrog.common.core.domain.AjaxResult;
import cn.sfrog.common.core.domain.ApiAjaxResult;
import cn.sfrog.common.core.page.TableDataInfo;
import cn.sfrog.common.enums.BusinessType;
import cn.sfrog.common.utils.poi.ExcelUtil;
import cn.sfrog.project.biz.admin.domain.vo.BizMessageLogFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizMessageLogQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizMessageLogVO;
import cn.sfrog.project.biz.admin.service.IBizMessageLogService;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


@Api(tags = "聊天消息记录接口")
@RestController
@RequestMapping("/biz/messageLog")
public class BizMessageLogController extends BaseController {
    @Resource
    private IBizMessageLogService bizMessageLogService;

    /**
     * 查询聊天消息记录列表
     */
    @ApiOperation("查询列表")
    @GetMapping("/list")
    public TableDataInfo list(BizMessageLogQueryVO queryVO) {
        startPage();
        List<BizMessageLogVO> list = bizMessageLogService.selectBizMessageLogList(queryVO);
        return getDataTable(list);
    }

    /**
     * 导出聊天消息记录列表
     */
    @ApiOperation("导出")
    @PreAuthorize("@ss.hasPermi('biz:messageLog:export')")
    @Log(title = "聊天消息记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizMessageLogQueryVO query) {
        List<BizMessageLogVO> list = bizMessageLogService.selectBizMessageLogList(query);
        ExcelUtil<BizMessageLogVO> util = new ExcelUtil<BizMessageLogVO>(BizMessageLogVO.class);
        util.exportExcel(response, list, "聊天消息记录数据");
    }

    /**
     * 获取聊天消息记录详细信息
     */
    @ApiOperation("获取详情")
    @PreAuthorize("@ss.hasPermi('biz:messageLog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bizMessageLogService.selectBizMessageLogById(id));
    }

    /**
     * 新增聊天消息记录
     */
    @ApiOperation("新增")
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('biz:messageLog:add')")
    @Log(title = "聊天消息记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizMessageLogFormVO form) {
        return toAjax(bizMessageLogService.insertBizMessageLog(form));
    }

    /**
     * 修改聊天消息记录
     */
    @ApiOperation("修改")
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('biz:messageLog:edit')")
    @Log(title = "聊天消息记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizMessageLogFormVO form) {
        return toAjax(bizMessageLogService.updateBizMessageLog(form));
    }

    /**
     * 删除聊天消息记录
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('biz:messageLog:remove')")
    @Log(title = "聊天消息记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bizMessageLogService.deleteBizMessageLogByIds(ids));
    }

    /**
     * 更改状态聊天消息记录
     */
    @ApiOperation("更改状态")
    @PreAuthorize("@ss.hasPermi('biz:messageLog:changeStatus')")
    @Log(title = "聊天消息记录", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody @Valid BizStatusChangeVO vo) {
        return AjaxResult.success(bizMessageLogService.changeStatus(vo));
    }

    /**
     * 根据会话id设置聊天消息已读
     * @param conversationId
     * @return
     */
    @ApiOperation("根据会话id设置聊天消息已读")
    @Log(title = "聊天消息记录", businessType = BusinessType.UPDATE)
    @PostMapping("/setMessageRead/{conversationId}")
    public AjaxResult setMessageReadByConversationId(@PathVariable  Long conversationId){
        return toAjax(bizMessageLogService.setMessageReadByConversationId(conversationId));
    }

    @ApiOperation(value = "设置消息已读")
    @Log(title = "聊天消息记录", businessType = BusinessType.UPDATE)
    @PostMapping("/setMessageReadById/{messageId}")
    public ApiAjaxResult setMessageRead(@PathVariable Long messageId){
        Boolean flag =  bizMessageLogService.setMessageRead(messageId);
        return ApiAjaxResult.success(flag);
    }

}
